<?php
/**
 * Created by PhpStorm.
 * User: mawernhuan
 * Date: 17/10/18
 * Time: 下午12:02
 */

namespace App\Utils;


use DateTime;

class DateUtil
{
    public static function now()
    {
        return date('Y-m-d H:i:s');
    }

    public static function today()
    {
        return date('Y-m-d');
    }

    public static function nowTime()
    {
        return date('H:i:s');
    }

    public static function yesterday()
    {
        return date('Y-m-d', time() - 86400);
    }

    /**
     * ------计算两个日期之间的差值多少天
     * @param $startDate
     * @param $endDate
     * @return mixed
     * @throws \Exception
     */
    public static function dateDifference($startDate, $endDate)
    {
        $datetimeStart = new DateTime($startDate);
        $datetimeEnd = new DateTime($endDate);
        $diffDays = $datetimeStart->diff($datetimeEnd)->days;
        return $diffDays;
    }

    /**
     * -----根据时间生成唯一随机数
     * @return string
     */
    public static function getRandomByTime()
    {
        $rand = (string) (time() . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT));

        return str_shuffle($rand);
    }

    public static function upDateFormat($data, $s = '-', $r = '.')
    {
        return str_replace($s, $r, $data);
    }

    public static function format($ts)
    {
        return date('Y-m-d H:i:s', $ts);
    }

    public static function datetimeDiff($first, $second)
    {
        return strtotime($first) - strtotime($second);
    }
}
